/*
 * @Author: 大白
 * @Date: 2019-12-04 09:48:26
 * @LastEditors: 杨振洲 yzz0412521@163.com
 * @LastEditTime: 2022-11-30 13:41:11
 * @Description: 文件描述
 * @FilePath: \cyf_admin\src\main.js
 */
import Vue from 'vue'
import App from './App'
import router from './router'
import store from "./store"
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from "axios"
import 'normalize.css/normalize.css'
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import Router from "vue-router"
import './directive/auth'
import echarts from 'echarts'
import '@/styles/index.scss' // global css
import Fragment from 'vue-fragment'
import lodash from "lodash"
import md5 from 'js-md5';
Vue.config.devtools = false;
//导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor';
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import "echarts/map/js/world.js";
import "echarts/map/js/china.js";
import "echarts/extension-src/bmap/bmap.js";
import 'babel-polyfill'
//日期格式处理
import moment from 'moment'
import fragment from 'vue-fragment'
import promise from 'es6-promise'
import observer from '@/utils/observer'
Vue.prototype.$observer = observer
promise.polyfill();
Vue.use(fragment.Plugin)
Vue.config.productionTip = false
//全局过滤器 - 处理日期
Vue.filter('fmtdate', (v) => {
 if (v) {
  return moment(v).format('YYYY-MM-DD HH:mm:ss')
 } else {
  return ""
 }
})
//全局过滤器 - 处理精确到日
Vue.filter('fmtday', (v) => {
 if (v) {
  return moment(v).format('YYYY-MM-DD')
 } else {
  return ""
 }

})
//全局过滤器 -处理金额
Vue.filter('money', (v) => {
 if (v) {
  v = v.toString();
  var result = (Number(v) / 100).toFixed(2);
  return result;
 } else {
  return 0
 }

})
// 首页过滤处理金额
Vue.filter('top', (v) => {
 if (v) {
  v = v.toString();
  var result = (Number(v) / 1000000).toFixed(2);
  return result;
 } else {
  return 0
 }

})
Vue.filter('phone', (s) => {
 if (s) {
  // let reg = /^(.{3}).*(.{4})$/
  // return val.replace(reg, '$1****$2')
  var obj = s.match(/((((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\d{8})|((\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}))?/g)

  var tel;
  var trueTel;
  for (var item in obj) {
   tel = obj[item];
   if (tel && tel.length == 11) {
    trueTel = tel.substr(0, 3) + '****' + tel.substr(7)
    s = s.replace(tel, trueTel);
   }
  }
  return s;
 } else {
  return ""
 }

})
//全局过滤器 -处理金额
Vue.filter('float2', (v) => {
 if (v) {
  v = v.toString();
  var result = (Number(v)).toFixed(2);
  return result;
 } else {
  return 0
 }

})
//全局过滤器 -处理使劲
Vue.filter('timeflag', (v) => {
 if (v) {
  var result = Number(v).toString().slice(0, Number(v).toString().length - 2) + ':00' + "~" + (Number(v) + 100).toString().slice(0, (Number(v) + 100).toString().length - 2) + ':00';
  return result;
 } else {
  return '**'
 }

})
Vue.use(ElementUI);
Vue.use(VueQuillEditor)
Vue.prototype.$axios = axios;
Vue.prototype.$echarts = echarts
Vue.prototype.$_ = lodash
Vue.prototype.$md5 = md5;
Vue.use(Fragment.Plugin)
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
 return originalPush.call(this, location).catch(err => err)
}
function GetRequest () {
 var url = location.search; //获取url中"?"符后的字串
 if (url.indexOf("?") != -1) {    //判断是否有参数
  var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
  var strs = str.split("=");   //用等号进行分隔 （因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔）
  return strs[1]         //直接弹出第一个参数 （如果有多个参数 还要进行循环的）
 }
}
let registerRouteFresh = true
router.beforeEach((to, from, next) => {
 console.log("next1")
 if (GetRequest("token")) {
  console.log("next2")

  let data = JSON.parse(decodeURIComponent(GetRequest("token")))
  store.dispatch("login/LOGIN", { token: data }).then(res => {
   store.dispatch('delAllViews')
   window.location.href = process.env.API_LOGIN_URL

  });
 }
 else {
  console.log("next3")

  if (!store.state.login.token && !store.state.login.sidebarMenu.length) {
   console.log("next4")

   if (to.path == '/login') {
    console.log("next5", process.env.API_CYF_URL)

    NProgress.start()
    window.location.href = process.env.API_CYF_URL
    next()
   } else {
    console.log("next6", process.env.API_CYF_URL)

    window.location.href = process.env.API_CYF_URL
    next('/login')
   }
  } else {
   if (registerRouteFresh) {
    console.log("next7")

    store.dispatch("login/SET_ROUTER").then(res => {
     registerRouteFresh = false
     NProgress.start()
     next()
    })
   } else {
    console.log("next8")

    NProgress.start()
    // console.log(to)
    if (to.path != '/index') {
     console.log("next9")

     store.dispatch("home/ADD_HISTORY", to)
    }
    next()
   }
  }
 }

})
router.afterEach((to) => {
 if (store.state.login.token && to.path == '/login') {
  router.replace('/')
  // router.replace('/shift_record')
 }
 if (to.fullPath !== '/login') {    //判断是否为登录页面

  // setWaterMark("", store.state.login.userInfo.name);
  // setWaterMark("", 'yingkouzhongzhengshiyou');

 }

 NProgress.done()
 var routerList = to.matched
 store.commit('home/SET_CRUMBLIST', routerList)

})
new Vue({
 el: '#app',
 router,
 store,
 components: { App },
 template: '<App/>'
})
